package com.me.mygdxgame.assets;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.assets.AssetErrorListener;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.utils.Disposable;

public class Assets implements Disposable, AssetErrorListener{
	
	public static final String TAG = Assets.class.getName();
	public static final Assets instance = new Assets();
	public AssetManager assetManager;
	
	public static TextureAtlas gameScreen;
	public static Texture coins, gun, potgold, item, stone1, stone2, stone3;
	public static TextureRegion player, live;
	public static TextureRegion rightMove, rightMove_press, leftMove, leftMove_press, jumpMove, jumpMove_press;
	
	public static BitmapFont defaultSmall;
	public static BitmapFont defaultNormal;
	public static BitmapFont defaultBig;
	
	private Assets(){}
	
	public void init(AssetManager assetManager){
		this.assetManager = assetManager;
		assetManager.setErrorListener(this);
		assetManager.load("gameScreen/gameScreen.pack", TextureAtlas.class);
		assetManager.load("gameScreen/Coins.png", Texture.class);
		assetManager.load("gameScreen/Tannenzapfen.png", Texture.class);
		assetManager.load("gameScreen/GoldKiste.png", Texture.class);
		assetManager.load("gameScreen/Item.png", Texture.class);
		assetManager.load("gameScreen/Stone_1_1.png", Texture.class);
		assetManager.load("gameScreen/Stone_1_2.png", Texture.class);
		assetManager.load("gameScreen/Stone_1_3.png", Texture.class);
//		assetManager.finishLoading();
		
		
	}
	
	public void takeResources(){
		gameScreen = assetManager.get("gameScreen/gameScreen.pack", TextureAtlas.class);
		for(Texture t : gameScreen.getTextures()){
			t.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		}
		coins = assetManager.get("gameScreen/Coins.png", Texture.class);
		coins.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		gun  = assetManager.get("gameScreen/Tannenzapfen.png", Texture.class);
		gun.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		potgold = assetManager.get("gameScreen/GoldKiste.png", Texture.class);
		potgold.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		item = assetManager.get("gameScreen/Item.png", Texture.class);
		item.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		stone1 = assetManager.get("gameScreen/Stone_1_1.png", Texture.class);
		stone1.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		stone2 = assetManager.get("gameScreen/Stone_1_2.png", Texture.class);
		stone1.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		stone3 = assetManager.get("gameScreen/Stone_1_3.png", Texture.class);
		stone3.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		
		player = gameScreen.findRegion("Player");
		live = gameScreen.findRegion("Pilz");
		
		leftMove = gameScreen.findRegion("Button_Left");
		leftMove_press = gameScreen.findRegion("Button_Left_p");
		rightMove = gameScreen.findRegion("Button_Right");
		rightMove_press = gameScreen.findRegion("Button_Right_p");
		jumpMove = gameScreen.findRegion("Button_Jump");
		jumpMove_press = gameScreen.findRegion("Button_Jump_p");
		
		defaultSmall = new BitmapFont(Gdx.files.internal("font/arial-15.fnt"), false);
		defaultNormal = new BitmapFont(Gdx.files.internal("font/arial-15.fnt"), false);
		defaultBig = new BitmapFont(Gdx.files.internal("font/arial-15.fnt"), false);
		defaultSmall.setScale(0.75f);
		defaultNormal.setScale(1f);
		defaultBig.setScale(2f);
		
		defaultSmall.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
		defaultNormal.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
		defaultBig.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
	}
	
	public static Texture loadTexture(String path){
		return new Texture(Gdx.files.internal(path));
	}
	
	public static TextureAtlas loadTextureAtlas(String path){
		return new TextureAtlas(Gdx.files.internal(path));
	}

	@Override
	public void error(AssetDescriptor asset	, Throwable throwable) {
		Gdx.app.error(TAG, "Coundn't load asset " + asset.toString());
	}

	@Override
	public void dispose() {
		assetManager.dispose();
		defaultSmall.dispose();
		defaultNormal.dispose();
		defaultBig.dispose();
	}

}
